<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8" isELIgnored="false"%>
<%@ include file="/WEB-INF/views/shared/taglib.jsp"%>
<%
	request.setAttribute("title", "找回密码");
%>

<%@ include file="/WEB-INF/views/shared/master/weContentHeader.jsp"%>
<style>
	.weui-cell__hd img{
		margin-right:15px;
	}
	
	.weui-cell:before{
		border-top:0px;
		
	}
	.weui-cell{
		padding:10px 20px 10px 0px;
		margin:0 auto;
		width:70%;
		border-bottom:1px solid #d9d9d9;	
	}
	
	.center-icon{
		width:125px;
		height:125px;
	}
	
	.imgBox{
		margin-top:10px;
		text-align:center;
	}
	
	.titlebox{
		margin-top:5px;
	}

	.bottom-lab {
		margin-left:44px;
		margin-top:60px;
		background: #fff;
		width: 80%;
		text-align: center;
	}
	.bottom-btn {
		margin-bottom: 15px;
		background-color: #2196F3;
		height:40px;
		line-height:40px;	
	}
	
	.registration{
		color:grey;
	}
	
	.toolbar .picker-button {
		color:#2196F3;
	}
	.btn-code{
		float:right;
	}
	.input-code{
		width:50%;
	}
	.btn-text{
		background-color:white;
		color:#2196F3;
		padding:5px;
		border:1px solid #2196F3;	
	}
	.btn-text:disabled{
		color:#ddd;
		border:1px solid #ddd;
	}
	.weui-toptips {
		height: 1.8rem !important;
		font-size: 1.1rem;
	}
	.color-red {
		color: red;
	}
</style>

<%@ include file="/WEB-INF/views/shared/master/weTitle.jsp"%>
<div class="goback" data-back-href="<c:url value='/wechat/acc/myWealth/myWealth'/>"></div>	

	<input type="hidden" name="custId" id="custId" />
	<div class="weui-cell" style="margin-top:3rem;">
			<div class="weui-cell__hd">
				<img class="left-icon" alt="" src="<c:url value="/images/we/registration/phone.png" />">
			</div>
			<div class="weui-cell__bd">
				<input id="phone" name="phone" class="weui-input selected-color" type="text" placeholder="请输入手机号" onBlur="checkPhone()">
			</div>
	</div>
	
	
	<div class="weui-cell">
			<div class="weui-cell__hd">
				<img class="left-icon" alt="" src="<c:url value="/images/we/registration/security-code.png" />">
			</div>
			<div class="weui-cell__bd getcode">
				<input id="code" name="code" class="weui-input selected-color input-code" type="text" placeholder="验证码" value="${code}">
				<div class="btn-code">
					<button type="button" class="btn-text" id="btn-get" onclick="getCode()">获取验证码</button>
				</div>
			</div>
	</div>
	
	<div class="weui-cell">
			<div class="weui-cell__hd">
				<img class="left-icon" alt="" src="<c:url value="/images/we/registration/password.png" />">
			</div>
			<div class="weui-cell__bd">
				<input id="password" name="password" class="weui-input selected-color" type="password" placeholder="输入新密码" onBlur="checkPassword()">
			</div>
	</div>
	
	<div class="weui-cell">
			<div class="weui-cell__hd">
				<img class="left-icon" alt="" src="<c:url value="/images/we/registration/password.png" />">
			</div>
			<div class="weui-cell__bd">
				<input id="retryPassword" name="password" class="weui-input selected-color" type="password" placeholder="再次输入密码" onBlur="checkRetryPassword()">
			</div>
	</div>
	
	<div class="bottom-lab">
		<button type="button" onclick="RetrievePassword()" class="weui-btn weui-btn_primary bottom-btn">确定</button>
	</div>

<%@ include file="/WEB-INF/views/shared/scripts/jquery.jsp"%>
<%@ include file="/WEB-INF/views/shared/scripts/jqweui.jsp"%>	
<script type="text/javascript"> 
	$(function(){
		var phoneWidth = $(window).width();

		//动态设置button的margin-left的值
		var btnWidth = $(".bottom-lab").width();
		var left = (phoneWidth-btnWidth)/2;
		$(".bottom-lab").css("margin-left",left);
		
	});
	
	function RetrievePassword(){
		
		if(!checkInput()){
			return;
		}
		var newPassword = $("#retryPassword").val();
		var customerId = $("#custId").val();
		var code = $("#code").val();
		var url = $.getVirtualPath()+"/wechat/cust/customerIden/updatePassword?newPassword="+newPassword+"&customerId="+customerId+"&code="+code;
		function checkCode(){
			var code = $("#code").val();
			var phone = $("#phone").val();
			if($.isNull(code)){
				$.toptip("请输入验证码","error");
				return false;
			}
			if (sendMessagePhone != phone) {
				$.toptip("验证码与手机号不匹配","error");
				return false;
			}
			return true;
		};
		var result=$.getTextPost(url);
		if(result=="true"){
			$.toast("修改成功");
			setTimeout(function(){
				window.location.href=$.getVirtualPath()+"/wechat/acc/myWealth/withdraw";	
			},400);			
		}else if(result=="expire"){
			$.toast("验证码过期", "forbidden");
		}else if(result=="error"){
			$.toast("验证码错误","forbidden");
		}else if(result=="false"){
			$.toast("修改失败", "forbidden");
		}
	}

	/* 验证手机号码 */
	function checkPhone(){
		var phone = $('#phone').val();
		var pattern = /^1[0-9]{10}$/;
		if(phone == '') {
			$.toptip("请输入手机号码","error");
			return false;
		}else if(!pattern.test(phone)){
			$.toptip("请输入正确的手机号码","error");
			return false;
		}
		return true;
	}
	
	/* 通过手机号码得到当前customer */
	function getCustomerByPhone(){
		var phoneNum = $("#phone").val();
		var url=$.getVirtualPath()+"/wechat/cust/customerIden/getCustomerNowByPhone";
		return $.getJsonPost(url,"phone="+phoneNum);
	}
	
	/*验证手机号是不是这个人的 */
	function checkPhoneIsHis(){
		if(!checkPhone()){
			return false;
		}	
		var customer = getCustomerByPhone();
		if($.isNull(customer)){
			$.toptip("号码和当前用户不匹配","error");
			return false;
		}
		$("#custId").val(customer.id);
		return true;
	}

	function sendMessage(){
	    var phoneNum = $("#phone").val();
	    sendMessagePhone = phoneNum;
	    var url=$.getVirtualPath()+"/wechat/cust/customerIden/sendMessage";
	    $.getText(url,"phone="+phoneNum);
		setCountdown();
	}

	
	/* 发送验证码 */
	function getCode(){
		if(checkPhone()){
			if(!checkPhoneIsHis()){
				return;
			}
			sendMessage();
		}else{
			$('#phone').focus();
		}
	}

	function setCountdown() {
		var $btnSend = $("#btn-get");
	    if (countdown == 0) { 
	    	$btnSend.attr('disabled',false); 
	    	$btnSend.text("获取验证码");
	        countdown = 60; 
	        return;
	    }
	    $btnSend.attr('disabled',true);
	    $btnSend.text("(" + countdown + ")s后重发");
	    countdown--;
	    setTimeout(function() { setCountdown($btnSend) },1000)
	}
	
	var countdown=60; 
	/* 验证码 */
	function checkCode(){
		var code = $("#code").val();
		var phone = $("#phone").val();
		if($.isNull(code)){
			$.toptip("请输入验证码","error");
			return false;
		}
		if (sendMessagePhone != phone) {
			$.toptip("验证码与手机号不匹配","error");
			return false;
		}
		return true;
	}
	//验证密码
	function checkPassword(){	
		var password=$("#password").val();
		var reg2 = /^[0-9]{6}$/;
		
		if($.isNull(password)){
			$.toptip("密码不可为空","error");
			return false;
		}else if(!reg2.test(password)){
			$.toptip("密码必须为6个数字","error");
			return false;
		}
		return true;
	}
	
	//验证密码是否一样
	function checkRetryPassword(){
		var password=$("#password").val();
		var retryPassword = $("#retryPassword").val();
		if(password!=retryPassword){
			$.toptip("两次输入密码不一样","error");
			return false;
		}
		return true;
	}
	
	//检查输入
	function checkInput(){
			if(checkPhone()&&
			   checkPassword()&&
			   checkRetryPassword()&&
			   checkCode()){
				return true;
			}
		return false;
	}
</script>


<%@ include file="/WEB-INF/views/shared/master/weContentFooter.jsp"%>
